<!-- UserModel.php-->
<?php 
	//require 'common/ConnDB.php';
	//require 'common/config.php';
	require 'Model.php';
	class FoodModel extends Model{
		function __Construct(){
			parent::__Construct();
		}
		function getfoodList(){
			$sql = 'select * from foods';
			$data = $this->conn->query($sql);
			//$this->conn = new ConnDB();
			return $data;
		}

		function getfoodinfor($foodname){
			$sql = "select * from foods where foodname like '%".$foodname."%' limit 0,10 ";;
			$data = $this->conn->query($sql);
			return $data;
		}

		function getUserByID($id){
			$sql = "select * from foods where fid=".$id;
			$data = $this->conn->query($sql);
			return $data;
		}

		function Updatefood($data){
			if(!empty($_FILES["pic"]["name"])){
  				$pic = parent::uploadfile('pic','../images/',array('png','jpg','gif'));
  			}
		    else{
		  		$pic='';
		  	}
		  	$food = array(
				'fid' =>$_POST['fid'],
				'foodname' => $_POST['foodname'],
				'tid' => $_POST['tid'],
				'pic' => $pic,
				'intro' => $_POST['intro'],
				'uploaddate' => $_POST['uploaddate'],
				);
			$sql="update foods set fid='$food[fid]',foodname='$food[foodname]',tid='$food[tid]',pic='$food[pic]',intro='$food[intro]',uploaddate='$food[uploaddate]' where fid=".$food['fid'];
			//echo "$sql";
			$result = $this->conn->query($sql);
			return $result;
		}

		function addfood($data){
			//如果选择了照片，则上传照片文件
			if(!empty($_FILES["pic"]["name"])){
  				$pic = parent::uploadfile('pic','../images/',array('png','jpg','gif'));
  			}
		    else{
		  		$pic='';
		  	}
			$user = array(
				'fid' =>'',
				'foodname' => $data['foodname'],
				'tid' => $data['tid'],
				'pic' => $pic,
				'intro' => $data['intro'],
				'uploaddate' => $data['uploaddate'],
				);
			$keys = implode(',',array_keys($user));
			$values = implode("','",array_values($user));
			/*$sql = "insert into users values ('$user[uid]','$user[uname]','$user[password]','$user[gender]','$user[birthdate]','$user[hobby]','$user[degree]','$user[intro]','$user[pic]')";*/
			$sql="insert into foods ({$keys}) VALUES ('{$values}')";
			//echo "$sql";
			$result = $this->conn->query($sql);
			return $result;
		}

		function Delfood($id){
			$sql = "Delete from foods where fid=".$id;
			$data = $this->conn->query($sql);
			return $data;
		}
	}
 ?>